# 设置环境变量

## 设置 BuildKit SBOM（Software Bill of Materials）扫描上下文为启用状态，用于生成软件物料清单。
ARG BUILDKIT_SBOM_SCAN_CONTEXT=true
## 设置编译镜像版本
ARG GO_VERSION=1.25.1
## 设置镜像架构
ARG PLATFORM=amd64
## 编译前端
ARG BUILD_JS=1

## 拼接镜像构建参数
ARG BUILDPLATFORM=linux/${PLATFORM}
ARG TARGETPLATFORM=linux-${PLATFORM}

# 编译前端
FROM node:23.11.1-bookworm AS js-builder

ARG BUILD_JS
# 换源
RUN yarn config set registry https://registry.npmmirror.com 

# 提前下载好依赖，部分依赖镜像无法下载
# COPY ./ts-调试/yl-依赖/app/node_modules /src/gotify/ui/node_modules

COPY ./Makefile /src/gotify/Makefile
COPY ./ui /src/gotify/ui

RUN if [ "$BUILD_JS" = "1" ]; then \
    (cd /src/gotify/ui && yarn install) && \
    (cd /src/gotify && make build-js) \
    else \
    mkdir -p /src/gotify/ui/build; \
    fi

# 编译后端，合并前端
FROM --platform=${BUILDPLATFORM} gotify/build:${GO_VERSION}-${TARGETPLATFORM} AS builder

# 设置国内镜像源
RUN go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
RUN sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \
    sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list 2>/dev/null && \
    sed -i 's|security.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \
    sed -i 's|security.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list 2>/dev/null

ARG BUILD_JS
ARG RUN_TESTS=0 # 0=never, 1=native only
ARG LD_FLAGS=""
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -yq --no-install-recommends \
    ca-certificates \
    git

COPY . /src/gotify
COPY --from=js-builder /src/gotify/ui/build /ui-build

RUN if [ "$BUILD_JS" = "1" ]; then \
    cp -r --update /ui-build /src/gotify/ui/build; \
    fi

RUN cd /src/gotify && \
    if [ "$RUN_TESTS" = "1" ] && [ "$BUILDPLATFORM" = "$TARGETPLATFORM" ]; then \
    go test -v ./...; \
    fi && \
    LD_FLAGS=${LD_FLAGS} make OUTPUT=/target/app/gotify-app _build_within_docker


# 最终镜像
FROM debian:13.1-slim

# 设置国内镜像源 - 适配 Debian 13 (trixie) slim 镜像
RUN sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \
    sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list 2>/dev/null && \
    sed -i 's|security.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \
    sed -i 's|security.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list 2>/dev/null

ARG GOTIFY_SERVER_EXPOSE=80
ENV GOTIFY_SERVER_PORT=$GOTIFY_SERVER_EXPOSE

WORKDIR /app

RUN export DEBIAN_FRONTEND=noninteractive && apt-get update && apt-get install -yq --no-install-recommends \
    tzdata \
    curl \
    ca-certificates && \
    rm -rf /var/lib/apt/lists/*

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s CMD curl --fail http://localhost:$GOTIFY_SERVER_PORT/health || exit 1
EXPOSE $GOTIFY_SERVER_EXPOSE

COPY --from=builder /target /

ENTRYPOINT ["./gotify-app"]

# docker buildx build -f ./docker/Dockerfile --platform linux/amd64 --tag firfe/gotify:2.7.3 --output type=docker .
# docker buildx build -f ./docker/Dockerfile --build-arg PLATFORM=arm64 --platform linux/arm64 --tag firfe/gotify:2.7.3-arm64 --output type=docker .
